/*
 ------------------------------------------------------------------------------
 Copyright (C) 2009-2011 Eternal Games.

 This file is part of the Eternal Tech source code.

 The Eternal Tech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Eternal Tech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Eternal Tech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// win_qgl.cpp - Binds all the GL to QGL function pointers
//


#include "../renderer/r_local.h"
#include "win_local.h"


typedef struct {
	HMODULE				hModule;

	bool				pointersCopied;

	FILE				*logFile;
	char				logFileName[MAX_PATH_LENGTH];
	int					logFileFrames;
	int					logFileNumber;
} qglState_t;

static qglState_t		qglState;


/*
 ==============================================================================

    FUNCTIONS LOGGING

 ==============================================================================
*/


static void QGL_CopyPointers ()
{
}

void QGL_EnableLogging (int enable)
{
	if (enable < 0 || enable > 1)
		return;   // Safety check
}

/*
 ==================
 QGL_LogPrintf
 ==================
*/
void QGL_LogPrintf (const char *fmt, ...)
{
	va_list	argPtr;

	if (!qglState.logFile)
		return;

	va_start(argPtr, fmt);
	vfprintf(qglState.logFile, fmt, argPtr);
	va_end(argPtr);
}


/*
 ==============================================================================

    FUNCTIONS BINDING

 ==============================================================================
*/


/*
 ==================
 QGL_GetProcAddress
 ==================
*/
static void *QGL_GetProcAddress (const char *procName)
{
	void	*procAddress;

	procAddress = GetProcAddress(qglState.hModule, procName);
	if (!procAddress)
	{
		FreeLibrary(qglState.hModule);
		qglState.hModule = NULL;

		Com_Error(ERR_FATAL, "QGL_GetProcAddress: GetProcAddress() failed for '%s'", procName);
	}

	return procAddress;
}

static void QGL_BindFunctions ()
{
}

static void QGL_UnbindFunctions ()
{
}


/*
 ==============================================================================

    INITIALIZATION AND SHUTDOWN

 ==============================================================================
*/


/*
 ==================
 QGL_Init
 ==================
*/
bool QGL_Init (const char *driver)
{
	char	name[MAX_PATH_LENGTH];

	Com_Printf("...initializing QGL\n");

	Str_Copy(name, driver, sizeof(name));
	Str_DefaultFileExtension(name, sizeof(name), LIBRARY_EXTENSION);

	Com_Printf("...calling LoadLibrary( '%s' ): ", name);
	if ((qglState.hModule = LoadLibrary(name)) == NULL)
	{
		Com_Printf("failed\n");
		return false;
	}
	Com_Printf("succeeded\n");

	QGL_BindFunctions();

	return true;
}

/*
 ==================
 QGL_Shutdown
 ==================
*/
void QGL_Shutdown ()
{
	Com_Printf("...shutting down QGL\n");

	if (qglState.logFile)
	{
		fclose(qglState.logFile);
		qglState.logFile = NULL;
	}

	if (qglState.hModule)
	{
		Com_Printf("...unloading OpenGL DLL\n");

		FreeLibrary(qglState.hModule);
		qglState.hModule = NULL;
	}

	memset(&qglState, 0, sizeof(qglState_t));

	QGL_UnbindFunctions();
}